package com.region.a.service;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import com.region.a.dto.TEST_ORDER;
import com.region.a.service.itf.A1002_ITF;
import com.syf.core.service.SyService;
import com.syf.core.service.SyServiceSetting;
import com.syf.core.service.context.ServiceContext;
import com.syf.kit.SyIdGenerator;
import com.syf.kit.SyfRdsDaoManager;
import com.syf.kit.core.dao.mapper.SyfMapper;

import java.util.Date;

@SyServiceSetting(code = "A1002", desc = "test-region-dao")
public class A1002 implements SyService<A1002_ITF.ReqData, A1002_ITF.RespData> {

    @Override
    public void execute(ServiceContext<A1002_ITF.ReqData, A1002_ITF.RespData> ctx) {
        A1002_ITF.ReqData reqData = ctx.getRequest().getDataObj();
        ctx.getTraceLog().info("收到请求，请求报文：{}", reqData);

        SyIdGenerator idGenerator = ctx.getToolKit(SyIdGenerator.class);
        String orderSeqNo = idGenerator.getUniqueSeqInCourse(3);

        SyfRdsDaoManager rdsDaoManager = ctx.getToolKit(SyfRdsDaoManager.class);

        TEST_ORDER orderEntity = TEST_ORDER.builder()
                .orderNo("T".concat(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_FORMAT)).concat(orderSeqNo))
                .orderName(reqData.getOrderName())
                .orderAmt(reqData.getOrderAmt())
                .orderDesc(reqData.getOrderDesc())
                .orderStatus("U0")
                .timeStamp(DateUtil.format(new Date(), DatePattern.PURE_DATETIME_MS_FORMAT))
                .build();
        SyfMapper<TEST_ORDER> test_order_dbMapper = rdsDaoManager.getMapper(TEST_ORDER.class);
        int result = test_order_dbMapper.insert(orderEntity);
        if (result < 1) {
            ctx.getResponse().setError("BIZ1001", "插入订单失败");
            return;
        }

        ctx.getResponse().setSuccess();
        ctx.getResponse().setDataObj(new A1002_ITF.RespData("我的返回参数1"));
    }
}
